﻿using BCCommon;
using BCCommon.Enums;
using BCDto.Common.Remark;
using BCDto.TMS.Agent.AgentRecord;
using BCService.TMS.Agent.AgentRecord;
using BCTMSRESTful.Configs;
using BCTMSRESTful.Controllers.Management;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BCTMSRESTful.Controllers.Management.Agent
{
    /// <summary>
    /// 代理商信息
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class AgentRecordController : ManagementBaseController
    {
        private readonly IAgentRecordService agentRecordService;

        public AgentRecordController(IAgentRecordService agentRecordService)
        {
            this.agentRecordService = agentRecordService;
        }

        /// <summary>
        /// 添加代理商信息
        /// </summary>
        /// <param name="requestDto">代理商信息</param>
        /// <response code="201">添加代理商信息</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加代理商信息")]
        [ProducesResponseType(typeof(AgentRecordDto), 201)]
        public IActionResult Post([FromBody] AgentRecordRequestDto requestDto)
        {
            var result = this.agentRecordService.Add(this.Admin, requestDto);
            return this.Ok(result);
        }

        /// <summary>
        /// 修改代理商信息
        /// </summary>
        /// <param name="agentRecordId">代理商记录Id</param>
        /// <param name="putDto">代理商信息</param>
        /// <response code="200">修改代理商信息</response>
        /// <returns></returns>
        [HttpPut("{agent_record_id}")]
        [Log("修改代理商信息")]
        [ProducesResponseType(typeof(AgentRecordDto), 200)]
        public IActionResult Put([FromRoute(Name = "agent_record_id")]long agentRecordId,
            [FromBody] AgentRecordPutDto putDto)
        {
            var result = this.agentRecordService.Update(this.Admin, agentRecordId, putDto);
            return this.Ok(result);
        }

        /// <summary>
        /// 删除代理商信息
        /// </summary>
        /// <param name="agentRecordId">代理商记录Id</param>
        /// <param name="remarkRequestDto">备注信息</param>
        /// <response code="204">删除代理商信息</response>
        /// <returns></returns>
        [HttpDelete("{agent_record_id}")]
        [Log("删除代理商信息")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "agent_record_id")] long agentRecordId,
            [FromBody] RemarkRequestDto remarkRequestDto)
        {
            this.agentRecordService.Delete(this.Admin, agentRecordId, remarkRequestDto);
            return NoContent();
        }

        /// <summary>
        /// 获取代理商信息
        /// </summary>
        /// <param name="companyId">公司Id</param>
        /// <param name="parentCompanyId">父级公司Id</param>
        /// <param name="agentLevel">代理商级别</param>
        /// <param name="isDelete">是否已删除</param>
        /// <param name="createStartDate">创建开始日期</param>
        /// <param name="createEndDate">创建结束日期</param>
        /// <param name="businessType">公司业务类型</param>
        /// <param name="companyName">公司名称</param>
        /// <param name="pageIndex">页号</param>
        /// <param name="pageSize">每页数据条数</param>
        /// <response code="200">获取代理商信息</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<AgentRecordViewDto>), 204)]
        public IActionResult Query([FromQuery(Name = "company_id")]int? companyId,
            [FromQuery(Name = "parent_company_id")]int? parentCompanyId,
            [FromQuery(Name = "agent_level")]int? agentLevel,
            [FromQuery(Name = "is_delete")]bool? isDelete,
            [FromQuery(Name = "create_start_date")]DateTime? createStartDate,
            [FromQuery(Name = "create_end_date")]DateTime? createEndDate,
            [FromQuery(Name = "business_type")] CompanyBusinessType? businessType,
            [FromQuery(Name = "company_name")] string companyName,
            [FromQuery(Name = "page_index")]int pageIndex = 1,
            [FromQuery(Name = "page_size")]int pageSize = 10)
        {
            var result = this.agentRecordService.Query(this.Admin, companyId, parentCompanyId, agentLevel, isDelete, createStartDate, createEndDate, businessType, companyName, pageIndex, pageSize);
            return this.Ok(result);
        }

        /// <summary>
        /// 修改代理商手动指派信息
        /// </summary>
        /// <param name="agentRecordId">代理商记录Id</param>
        /// <param name="putDto">代理商手动指派信息</param>
        /// <response code="200">修改代理商手动指派信息</response>
        /// <returns></returns>
        [HttpPut("[action]/{agent_record_id}")]
        [Log("修改代理商手动指派信息")]
        [ProducesResponseType(typeof(AgentRecordDto), 200)]
        public IActionResult ManualAssign([FromRoute(Name = "agent_record_id")] long agentRecordId,
            [FromBody] AgentRecordManualAssignDto putDto)
        {
            var result = this.agentRecordService.UpdateManualAssign(this.Admin, agentRecordId, putDto);
            return this.Ok(result);
        }

        /// <summary>
        /// 获取代理商信息
        /// </summary>
        /// <response code="200">获取代理商信息</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(AgentRecordDto), 200)]
        public IActionResult Get()
        {
            if (Admin.AgentRecord == null) 
            {
                throw new ArgumentException(" 您不是代理商，请更换代理商账号! ");
            }
            var result = this.agentRecordService.GetById(this.Admin.AgentRecord.AgentRecordId);
            return this.Ok(result);
        }
    }
}
